package com.lej.theme.dialog

import android.content.Context
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import androidx.core.view.isVisible
import com.lej.theme.databinding.ThemeDialogCommonBinding
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog

/**
 *  author: cjq
 *  date  : 2022/10/4 0004
 *  desc  :
 */
class CommonDialog(val context: Context) {
    fun showCommonDialog(
        title: CharSequence? = "提示",
        content: CharSequence? = "",
        longContent:CharSequence? = "",
        confirmText: CharSequence = "确认",
        cancelText: CharSequence = "取消",
        showClose: Boolean = true,//显示关闭按钮
        hasClose: Boolean = true,//是否可以点击空白关闭弹窗
        confirmClose: Boolean = true,//确定按钮点击后是否关闭
        cancel: () -> Unit = {},
        onConfirm: () -> Unit = {}
    ):QMUITipDialog {
        val mDialog = createDialog(context,title,content, longContent,confirmText, cancelText, showClose, confirmClose, cancel, onConfirm)
        mDialog.setCanceledOnTouchOutside(hasClose)
        mDialog.show()
        return mDialog
    }

    private fun createDialog(
        context: Context,
        title: CharSequence? = "提示",
        content: CharSequence? = "",
        longContent:CharSequence? = "",
        confirmText: CharSequence = "确认",
        cancelText: CharSequence = "取消",
        showClose: Boolean = true,//显示关闭按钮
        confirmClose: Boolean = true,//确定按钮点击后是否关闭
        cancel: () -> Unit = {},
        onConfirm: () -> Unit = {}
    ): QMUITipDialog {
        val dialog = QMUITipDialog(context)
        val binding = ThemeDialogCommonBinding.inflate(LayoutInflater.from(context),null,false)
        val width = (QMUIDisplayHelper.getScreenWidth(context) * 0.55).toInt()
        if(content?.isNotEmpty() == true){
            binding.scrollview.isVisible = false
            binding.tvContent.text = content
        }else{
            binding.scrollview.isVisible = true
            binding.tvContent.isVisible = false
            binding.tvLongContent.text = longContent
            binding.tvLongContent.movementMethod = LinkMovementMethod.getInstance()
        }
        binding.cl.layoutParams.width = width
        binding.cl.layoutParams.height = (width * (500f/790)).toInt()
        binding.tvCancel.text = cancelText
        binding.tvConfirm.text = confirmText
        binding.ivClose.isVisible = showClose
        binding.ivClose.onClick {
            dialog.dismiss()
        }
        binding.tvTitle.text = title?:""
        binding.tvCancel.onClick {
            cancel()
            dialog.dismiss()
        }
        binding.tvConfirm.onClick {
            onConfirm()
            dialog.dismiss()
        }
        dialog.setContentView(binding.root)
        return dialog
    }
}